package com.base.cn.platform.os.service.user.member;


import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.user.member.CusUserMemberDao;
import com.base.cn.platform.os.entity.user.member.CusUserMember;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户会员Biz
 * @author ll
 * @create 2018-04-26
 */
@Service
public class CusUserMemberBiz extends BaseBiz<CusUserMember,CusUserMemberDao> {

    /**
     * 保存修改用户会员
     *
     * @param userMember
     * @return
     */
    public CusUserMember saveUpdateUserMember(CusUserMember userMember) {
        if (DataUtil.idIsNotNull(userMember.getId())) {
            CusUserMember _userMember = this.findById(userMember.getId());
            _userMember.setDepict(userMember.getDepict());
            _userMember.setMemberName(userMember.getMemberName());
            _userMember.setMemberType(userMember.getMemberType());
            _userMember.setPrice(userMember.getPrice());
            _userMember.setValidDay(userMember.getValidDay());
            _userMember.setProductId(userMember.getProductId());
            this.updateById(_userMember,null);
            return _userMember;
        } else {
            userMember.setStatus(1);
            this.save(userMember);
            return userMember;
        }
    }

    /**
     * 修改会员状态
     *
     * @param id 会员ID
     * @param status       会员状态
     * @return Map<String,Object>
     */
    public void updateUserMemberStatus(BigDecimal id, Integer status) {
        CusUserMember userMember = new CusUserMember();
        userMember.setId(id);
        userMember.setStatus(status);
        this.updateById(userMember, null);
    }

    /**
     * 通过ID串查询会员商品列表并转Map
     * @param ids 会员商品列表
     * @return  Map<BigDecimal,CusUserMember>
     */
    public Map<BigDecimal,CusUserMember> findUserMemberToMapByIds(String ids){
        List<CusUserMember> userMemberList  = this.findUserMemberByIds(ids);
        if(ObjectUtils.isNotEmpty(userMemberList)){
            return userMemberList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
        }
        return null;
    }

    /**
     * 通过ID串，查询会员商品列表
     * @param ids 会员商品ID串
     * @return List<CusUserMember>
     */
    public List<CusUserMember> findUserMemberByIds(String ids){
        CusUserMember userMember = new CusUserMember();
        userMember.setMemberIds(ids);
        return this.queryUserMemberList(userMember);
    }

    /**
     * 查询列表
     * @param userMember
     * @return
     */
    public List<CusUserMember> queryUserMemberList(CusUserMember userMember){
        String where = whereSql(userMember);
        return this.find(where,null,null);
    }

    /**
     * 分页查询
     * @param page
     * @param userMember
     * @return
     */
    public PageInfo<CusUserMember> queryUserMemberListPage(Pagination page,CusUserMember userMember){
        String whereSql =whereSql(userMember);
        return this.findPage(whereSql,page,null);
    }

    /**
     * sql 返回
     * @param userMember
     * @return
     */
    public String  whereSql(CusUserMember userMember){
        StringBuilder whereSql = new StringBuilder(" 1=1");
        if(ObjectUtils.isNotEmpty(userMember)){
            if(userMember.getStatus()!=null&&userMember.getStatus()>0){
                whereSql .append( " and status = ").append(userMember.getStatus());
            }
            if(userMember.getMemberType()!=null&&userMember.getMemberType()>0){
                whereSql .append( " and memberType = ").append(userMember.getMemberType());
            }
            if(ObjectUtils.isNotEmpty(userMember.getSource())){
                whereSql .append( " and productId  is not null ");
            }
            if(StringUtils.isNotEmpty(userMember.getMemberIds())){
                String memberIds = StringUtils.subHeadTailString(userMember.getMemberIds(),",");
                whereSql .append(" and id in (").append(memberIds).append(")");
            }
        }
        return whereSql.toString();
    }


}


